{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（一）利用递归方法求5!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*  利用递归尽心计算[一般逆序收敛方向去考虑]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153\n"
     ]
    }
   ],
   "source": [
    "result=0\n",
    "def recursive(n):\n",
    "    inti_value=0\n",
    "    if n ==0 :\n",
    "        inti_value=1\n",
    "    else :\n",
    "        inti_value=n*recursive(n-1)\n",
    "    return inti_value\n",
    "total=0\n",
    "for i in range(1,6):\n",
    "    total+=recursive(i)\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（二）利用递归函数调用方式，将所输入的5个字符，以相反顺序打印出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入5个字符123ere\n",
      "r\n",
      "e\n",
      "3\n",
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "s=input('请输入5个字符')\n",
    "def output_str(s,l):\n",
    "    if l==0 :\n",
    "        return\n",
    "    else:\n",
    "        print(s[l-1])\n",
    "        output_str(s,l-1)\n",
    "output_str(s,5)       \n",
    "           \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（三）按逗号分隔列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1,2,3,4,5'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L  = [1,2,3,4,5]\n",
    "result = [str(item) for item in L]\n",
    "','.join(result)\n",
    "   \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（四）将一个数组逆序输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 3, 1]\n"
     ]
    }
   ],
   "source": [
    "l = [1,2,3,4,5]\n",
    "print(l[::-2])   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（五）两个 3 行 3 列的矩阵，实现其对应位置的数据相加，并返回一个新矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[2, 5, 6], [3, 6, 8], [8, 9, 12]]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp1=[[1,2,3],\n",
    "       [2,3,4],\n",
    "       [4,5,6]]\n",
    "temp2=[[1,3,3],\n",
    "       [1,3,4],\n",
    "       [4,4,6]]\n",
    "total=[[0,0,0],\n",
    "       [0,0,0],\n",
    "       [0,0,0]]\n",
    "for i in range(3):\n",
    "    for j in range(3):\n",
    "         total[i][j]=temp1[i][j]+temp2[i][j]\n",
    "total\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（六）匿名函数求和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python 使用 lambda 来创建匿名函数。\n",
    "lambda只是一个表达式，函数体比def简单很多。\n",
    "lambda的主体是一个表达式，而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。\n",
    "lambda函数拥有自己的命名空间，且不能访问自有参数列表之外或全局命名空间里的参数。\n",
    "虽然lambda函数看起来只能写一行，却不等同于C或C++的内联函数，后者的目的是调用小函数时不占用栈内存从而增加运行效率。\n",
    "语法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "sum=lambda a,b:a+b\n",
    "print(sum(1,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（七）查找字符串的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test=\"guochao1212\"\n",
    "test.find(\"12\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（八）在字典中找到年龄最大的人，并输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n"
     ]
    }
   ],
   "source": [
    "people = {'laowang':40,'tangyudi':30,'zhangsan':45}\n",
    "temp_list=people.values();\n",
    "age_max=max(temp_list)\n",
    "for i in people.keys():\n",
    "     if people[i]==age_max :\n",
    "            print(i)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（九）列表转换为字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'guochao': 0, 'is': 1, 'people': 2}\n"
     ]
    }
   ],
   "source": [
    "temp_list=[\"guochao\",\"is\",\"people\"]\n",
    "temp_dict={}\n",
    "for i in range(len(temp_list)):\n",
    "      temp_dict[temp_list[i]]=i\n",
    "print(temp_dict)        \n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（十）从键盘输入一个字符串，将小写字母全部转换成大写字母，然后输出到一个磁盘文件\"test\"中保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing test1.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile test1.txt\n",
    "111111111111112"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入一个串串gucoahooaao\n"
     ]
    }
   ],
   "source": [
    "result=input('输入一个串串')\n",
    "result=result.upper()\n",
    "with open(\"test1.txt\",\"a\") as f :\n",
    "    f.write(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
